package com.system.healthproject.model.sickuser.controller;

import com.system.healthproject.annotation.RequireToken;

import com.system.healthproject.bean.*;
import com.system.healthproject.common.ApiResult;
import com.system.healthproject.model.sickuser.service.SickPatientActivityService;
import com.system.healthproject.pojo.PatientActivity;
import com.system.healthproject.pojo.PatientActivitySignUp;
import com.system.healthproject.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author Xuheng Zhou
 * @date 2020/6/14 15:37
 * @desc
 */
@Controller
@RequestMapping("/SickUser/patientActivity")
@Api(value = "患者端-患教模块",tags = "患者端-患教管理API")
public class SickPatientActivityController {

    @Autowired
    private SickPatientActivityService service;

    @Autowired
    RedisUtil redisUtil;

    @RequireToken
    @ApiOperation(value = "查询患者的患教信息接口(已调试)",notes = "查询患者的患教信息",httpMethod = "post")
    @RequestMapping(value = "/selectSickPatientActivity",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页数",dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "页数显示数据条数",dataType = "Integer")})
    public ApiResult<PagesBean<SickUpPatientActivityBean>> selectSickPatientActivity(@RequestHeader("token") String token,
                                                                                     @RequestParam(name = "page") Integer page ,
                                                                                     @RequestParam(name = "size") Integer size){

       //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的患者登录id
        Integer loginId=(Integer) stringObjectMap.get("loginId");

        //通过loginId获取患者id
        Integer patientId =service.selectPatientIdByLoginId(loginId);

        List<SickUpPatientActivityBean> list=service.selectPatientIdPatientActivity(patientId,page,size);

        PagesBean pagesBean=new PagesBean();
        pagesBean.setData(list);
        pagesBean.setPageSize(size);
        pagesBean.setNowsPage(page);
        return ApiResult.successWithData("查询成功",pagesBean);
    }

    @RequireToken
    @ApiOperation(value = "查询患者已报名的患教信息接口(已调试)",notes = "查询患者已报名的患教信息",httpMethod = "post")
    @RequestMapping(value = "/selectSickSignUpPatientActivity",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页数",dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "页数显示数据条数",dataType = "Integer")})
    public ApiResult<PagesBean<SickSignUpPatientActivityBean>> selectSickSignUpPatientActivity(@RequestHeader("token") String token,
                                                                                               @RequestParam(name = "page") Integer page ,
                                                                                               @RequestParam(name = "size") Integer size){

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的患者登录id
        Integer loginId=(Integer) stringObjectMap.get("loginId");

        //通过loginId获取患者id
        Integer patientId =service.selectPatientIdByLoginId(loginId);

        List<SickSignUpPatientActivityBean> list=service.selectPatientIdSignUpPatientActivity(patientId,page,size);

        PagesBean pagesBean=new PagesBean();
        pagesBean.setData(list);
        pagesBean.setPageSize(size);
        pagesBean.setNowsPage(page);
        return ApiResult.successWithData("查询成功",pagesBean);
    }

    /**
     * 患者查询患教详情信息
     * @param patientactivityId
     * @return
     */
    @ApiOperation(value = "患者查询患教详情接口（已调试）",notes = "患者查询患教详情信息",httpMethod = "post")
    @RequestMapping(value = "/selectSickOnePatientActivity",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "patientactivityId",value = "患教活动id",dataType = "Integer")
    public ApiResult<SickOnePatientActivityBean> selectSickOnePatientActivity(@RequestParam("patientactivityId") Integer patientactivityId){

        System.out.println(patientactivityId);


        SickOnePatientActivityBean sickOnePatientActivityBean=new SickOnePatientActivityBean();

        sickOnePatientActivityBean=service.selectSickOnePatientActivity(patientactivityId);

        return ApiResult.successWithData("查询成功",sickOnePatientActivityBean);
    }


    @RequireToken
    @ApiOperation(value = "患者报名患教接口（已调试）",notes = "患者报名患教",httpMethod = "post")
    @RequestMapping(value = "/SickSignUpPatientActivity",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "patientactivityId",value = "患教活动id",dataType = "Integer")
    public ApiResult SickSignUpPatientActivity(@RequestHeader("token") String token,
                                               @RequestParam("patientactivityId") Integer patientactivityId){

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的患者登录id
        Integer loginId=(Integer) stringObjectMap.get("loginId");

        //通过loginId获取患者id
        Integer patientId =service.selectPatientIdByLoginId(loginId);


        //查询报名表里是否存在该患者的报名信息
        PatientActivitySignUp patientActivitySignUp=new PatientActivitySignUp();

        patientActivitySignUp=service.selectPatientActivitySignUpIsNull(patientactivityId,patientId);

        //判断该活动是否是已经结束或者取消的活动
        //如果是结束或者取消，则无法报名

        PatientActivity patientActivity=service.checkPatientActivity(patientactivityId);
        //报名结束时间
        Date endtime=patientActivity.getEndtime();
        //直播结束时间
        Date liveEndTime=patientActivity.getLiveendtime();
        //取消时间
        Date cancelTime=patientActivity.getLivecanceltime();
        //状态
        Integer state=patientActivity.getPatientactivityStatue();

        ApiResult apiResult = new ApiResult();
        //当前时间要小于报名时间，并且直播结束时间或者取消时间或者类型不是结束和取消，则可以进行报名
        if (endtime.before(new Date()) || liveEndTime!=null || cancelTime!=null || state==2 || state==3) {
            apiResult.setCode(500);
            apiResult.setMsg("该活动报名已截止或者活动已结束或已取消，无法报名！");
        }else {

            //如果查询报名表为空，说明没有报名，则可以报名
            if (patientActivitySignUp == null) {

                boolean tf = service.SickSignUpPatientActivity(patientId, patientactivityId);

                if (tf) {
                    apiResult.setCode(200);
                    apiResult.setMsg("报名成功");
                    return apiResult;

                } else {
                    apiResult.setCode(500);
                    apiResult.setMsg("报名失败！请联系管理员检查报名人数是否已满");
                    return apiResult;
                }
            } else {
                apiResult.setCode(500);
                apiResult.setMsg("报名失败！您已报名！请联系管理员！");
                return apiResult;
            }
        }
        return apiResult;
    }

    @ApiOperation(value = "患者查询直播开始的患教详情接口（已调试）",notes = "患者查询直播已开始患教详情信息",httpMethod = "post")
    @RequestMapping(value = "/selectSickOneIsLivePatientActivity",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "patientactivityId",value = "患教活动id",dataType = "Integer")
    public ApiResult<SickOneIsLivePatientActivity> selectSickOneIsLivePatientActivity(@RequestParam("patientactivityId") Integer patientactivityId){

        System.out.println(patientactivityId);


        SickOneIsLivePatientActivity sickOneIsLivePatientActivity=new SickOneIsLivePatientActivity();

        sickOneIsLivePatientActivity=service.selectSickOneIsLivePatientActivity(patientactivityId);

        return ApiResult.successWithData("查询成功",sickOneIsLivePatientActivity);
    }

}
